import { Controller } from "egg";

export default class PrivilegesTypeController extends Controller {
  public async index() {
    const { ctx } = this;
    ctx.body = await ctx.service.privilegesType.findList();
  }

  public async create() {
    const { ctx, service } = this;
    const { privileges_name } = ctx.request.body;
    const data = await service.privilegesType.findByName(privileges_name);
    if (data) {
      return ctx.helper.error({ ctx, errorType: "commonAlreadyExists" });
    }
    const res = await service.privilegesType.add(ctx.request.body);
    ctx.helper.success({ ctx, res });
  }

  public async createType() {
    const { ctx, service } = this;
    const { privileges_name } = ctx.request.body;
    const data = await service.privilegesType.findByNameType(privileges_name);
    if (data) {
      return ctx.helper.error({ ctx, errorType: "commonAlreadyExists" });
    }
    const res = await service.privilegesType.addType(ctx.request.body);
    ctx.helper.success({ ctx, res });
  }
}
